﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
//using CoreEMS.BLL;
//using CoreEMS.Entities;
//using Common.PscMessages;

namespace CMS_GYM
{
    public partial class uctReportManagements : UserControl
    {
        #region "Declare Variables"
        clsReportTemplatesDTO _reportTemplateInfo = new clsReportTemplatesDTO();
        clsReportTemplatesBUS ReportTemplatesBUS = new clsReportTemplatesBUS();
        DataTable _dtData = new DataTable();
        #endregion

        #region "Initials"
        #region uctReportManagements()
        public uctReportManagements()
        {
            InitializeComponent();

            uct_Reset.Reset.Click += new EventHandler(btn_Reset_Click);
            uct_Save.Save.Click += new EventHandler(btn_Save_Click);
            uct_Delete.Delete.Click += new EventHandler(btn_Delete_Click);
            uct_Close.Close.Click += new EventHandler(btn_Close_Click);
            uct_Print.Print.Click += new EventHandler(btn_Print_Click);
        }
        #endregion
        #endregion

        #region "Functions"
        #region LoadData()
        public void LoadData()
        {
            try
            {
                _dtData = ReportTemplatesBUS.GetReportTemplates();
                DataView dv = new DataView(_dtData);
                dv.Sort = "ReportTemplate";
                dtg_Data.DataSource = dv;
                dtg_Data.Columns["ReportTemplate"].HeaderText = "MS";
                dtg_Data.Columns["ReportTemplateName"].HeaderText = "Tên mẫu";
                dtg_Data.Columns["ReportFileName"].HeaderText = "Tên File";
                dtg_Data.Columns["ReportTemplateXml"].Visible = false;
                dtg_Data.Columns["Note"].HeaderText = "Ghi chú";
                dtg_Data.Columns["IsInUse"].Visible = false;

                dtg_Data_Resize(null, null);
            }
            catch { }
        }
        #endregion
        #endregion

        #region "Events"
        #region btn_Close_Click(object sender, EventArgs e)
        private void btn_Close_Click(object sender, EventArgs e)
        {
            
        }
        #endregion

        #region btn_Reset_Click(object sender, EventArgs e)
        private void btn_Reset_Click(object sender, EventArgs e)
        {
            txt_ReportTemplate.Text = string.Empty;
            txt_ReportTemplateName.Text = string.Empty;
            txt_ReportFileName.Text = string.Empty;
            txt_Note.Text = string.Empty;
            _reportTemplateInfo = new clsReportTemplatesDTO();
            txt_ReportTemplateName.Focus();
            txtXML.Text = string.Empty;
        }
        #endregion

        #region btn_Save_Click(object sender, EventArgs e)
        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                clsReportTemplatesDTO reportTemplate = new clsReportTemplatesDTO();
                reportTemplate = _reportTemplateInfo;
                reportTemplate.ReportTemplateName = txt_ReportTemplateName.Text.Trim();
                reportTemplate.ReportFileName = txt_ReportFileName.Text.Trim();
                reportTemplate.Note = txt_Note.Text.Trim();
                reportTemplate.ReportTemplateXml = txtXML.Text.Trim();
                reportTemplate.IsInUse = true;

                if (reportTemplate.ReportTemplate == 0)
                {
                    int result = ReportTemplatesBUS.Them(reportTemplate);
                    //int result = 0;
                    if (result > 0)
                    {
                        MessageBox.Show("Thêm mới dữ liệu báo cáo thành công", "Thông báo");
                        reportTemplate.ReportTemplate = result;
                        _reportTemplateInfo = reportTemplate;

                        _dtData.Rows.Add(new object[] { reportTemplate.ReportTemplate, reportTemplate.ReportTemplateName
                            , reportTemplate.ReportFileName, reportTemplate.ReportTemplateXml, reportTemplate.Note, true });
                    }
                    else MessageBox.Show("Lỗi thêm mới dữ liệu báo cáo", "Thông báo");
                }
                else
                {
                    int result = ReportTemplatesBUS.Sua(reportTemplate);
                    //int result = 0;
                    //PscMessage.ShowResult(result, "Cập nhật");
                    if (result == 0)
                    {
                        MessageBox.Show("Cập nhật dữ liệu báo cáo thành công", "Thông báo");
                        _reportTemplateInfo = reportTemplate;
                        DataRow[] drSelect = _dtData.Select("ReportTemplate = " + reportTemplate.ReportTemplate.ToString());
                        drSelect[0]["ReportTemplateName"] = reportTemplate.ReportTemplateName;
                        drSelect[0]["ReportFileName"] = reportTemplate.ReportFileName;
                        drSelect[0]["ReportTemplateXml"] = reportTemplate.ReportTemplateXml;
                        drSelect[0]["Note"] = reportTemplate.Note;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        #endregion

        #region btn_Delete_Click(object sender, EventArgs e)
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (_reportTemplateInfo.ReportTemplate != 0
                    && MessageBox.Show("Xác nhận xóa mẫu biểu '" + _reportTemplateInfo.ReportTemplate.ToString() + " : " + _reportTemplateInfo.ReportTemplateName + "' ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    int result = ReportTemplatesBUS.Xoa(_reportTemplateInfo.ReportTemplate);
                    //PscMessage.ShowResult(result, "Xóa");
                    if (result == 0)
                    {
                        DataRow[] drSelect = _dtData.Select("ReportTemplate = " + _reportTemplateInfo.ReportTemplate.ToString());
                        drSelect[0].Delete();
                        btn_Reset_Click(null, null);
                    }
                }
            }
            catch { }
        }
        #endregion

        #region btn_ReportTemplateXml_Click(object sender, EventArgs e)
        private void btn_ReportTemplateXml_Click(object sender, EventArgs e)
        {
            panelXML.Visible = true;
        }
        #endregion

        #region btn_Print_Click(object sender, EventArgs e)
        private void btn_Print_Click(object sender, EventArgs e)
        {
            try
            {
                //DataTable dt = BLReportTemplates.sp_rpt_DanhSachBaoCao();
                //frmXemBaoCao f = new frmXemBaoCao();
                //f.uct_ReportViewer.InDanhSachBaoCao(dt);
                //f.ShowDialog();
            }
            catch { }
        }
        #endregion

        #region dtg_Data_RowEnter(object sender, DataGridViewCellEventArgs e)
        private void dtg_Data_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtg_Data.SelectedRows.Count == 0) return;
                _reportTemplateInfo = new clsReportTemplatesDTO();
                DataGridViewRow row = dtg_Data.SelectedRows[0];
                _reportTemplateInfo.ReportTemplate = int.Parse(row.Cells["ReportTemplate"].Value.ToString());
                _reportTemplateInfo.ReportTemplateName = row.Cells["ReportTemplateName"].Value.ToString();
                _reportTemplateInfo.ReportFileName = row.Cells["ReportFileName"].Value.ToString();
                _reportTemplateInfo.ReportTemplateXml = row.Cells["ReportTemplateXml"].Value.ToString();
                _reportTemplateInfo.Note = row.Cells["Note"].Value.ToString();
                _reportTemplateInfo.IsInUse = (row.Cells["IsInUse"].Value.ToString().ToUpper() == "TRUE");

                txt_ReportTemplate.Text = _reportTemplateInfo.ReportTemplate.ToString();
                txt_ReportTemplateName.Text = _reportTemplateInfo.ReportTemplateName;
                txt_ReportFileName.Text = _reportTemplateInfo.ReportFileName;
                txtXML.Text = _reportTemplateInfo.ReportTemplateXml;
                txt_Note.Text = _reportTemplateInfo.Note;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi : " + ex.Message, "Thông báo");
            }
        }
        #endregion

        #region dtg_Data_Resize(object sender, EventArgs e)
        private void dtg_Data_Resize(object sender, EventArgs e)
        {
            try
            {
                if (dtg_Data.DataSource == null) return;
                dtg_Data.Columns["ReportTemplate"].Width = 50;
                dtg_Data.Columns["ReportTemplateName"].Width = (dtg_Data.Width - 70) / 3;
                dtg_Data.Columns["ReportFileName"].Width = (dtg_Data.Width - 70) / 3;
                dtg_Data.Columns["Note"].Width = dtg_Data.Width - (((dtg_Data.Width - 70) / 3) * 2) - 70;
            }
            catch { }
        }
        #endregion

        #region btn_Export_Click(object sender, EventArgs e)
        private void btn_Export_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    if (_reportTemplateInfo.ReportTemplate == 0) return;
            //    using (StreamWriter sw = new StreamWriter(CoreEMS.User._dataDirectory + @"\Reports\" +  _reportTemplateInfo.ReportFileName + ".rdlc"))
            //    {
            //        sw.Write(_reportTemplateInfo.ReportTemplateXml);
            //    }
            //    PscMessage.Show("Tạo file thành công...\n" + CoreEMS.User._dataDirectory + @"\Reports\" + _reportTemplateInfo.ReportFileName + ".rdlc", "EMS - Thông báo", ListButton.Close, ICon.Success);
            //}
            //catch (Exception ex)
            //{
            //    PscMessage.Show(ex.Message, "ERROR", ListButton.Close, ICon.Error);
            //}
        }
        #endregion

        #region btnOK_Click(object sender, EventArgs e)
        private void btnOK_Click(object sender, EventArgs e)
        {
            panelXML.Visible = false;
            if (txtXML.Text != string.Empty) 
                _reportTemplateInfo.ReportTemplateXml = txtXML.Text.Trim();
        }
        #endregion
        #endregion
    }
}
